<?php
	require 'Service/Config/config.php';




//-- 信息删除处理
//-- 信息发布处理
//-- 验证码
//-- token设置

	

//dump() 展示数据
//I() 过滤POST,GET数据
//expire_v() 验证时间是否过期
//S() 缓存数据
//alert() 弹出提示并返回上页
//get_today_start_timestamp() 获取当天起始的时间戳






	switch($_GET['type']){

		case 'img_record_stamp':	//设置临时图片的保存时间分割点
			session_start();
			if(!token_v()){
				echo 'error1';exit;
			}
			$stamp = I('stamp');
			file_put_contents('Service/Template/img_record_stamp',$stamp);
		break;

		case 'img_record_update':				//删除数据库中过期的临时图片，并更新时间戳
			session_start();
			if(!token_v()){
				echo 'error2';exit;
			}
			$stamp = I('stamp');
			require 'Service/Class/MyPdo.class.php';
			$m_Pdo = new MyPdo();
			$sql = 'delete from service_img_record where ctime < ?';
			$res = $m_Pdo->dml($sql,array($stamp));
			file_put_contents('Service/Template/img_record_stamp',$stamp);
		break;


		case 'issue_img_del':
			session_start();
			if(!token_v()){
				exit('error3');
			}
			$del_src = I('post.del_src');
			$img_arr = json_decode($_SESSION['service_img_record']);
			$index = array_search($del_src,$img_arr);
			if($index === 0 || $index){
				array_splice($img_arr,$index,1);
				$_SESSION['service_img_record'] = json_encode($img_arr);
				$path = '/imgs/'.$del_src;
				require 'Service/Class/upyun.class.php';
				$upyun = new UpYun();
				$res = $upyun->delete($path);
				if($res){
					require 'Service/Class/MyPdo.class.php';
					$mypdo = new MyPdo();
					$sql = "delete from service_img_record where name = ?";
					echo $res2 = $mypdo->dml($sql,array($del_src));
				}
			}
		break;






		case 'img_upload':
			session_start();
			if(!token_v()){
				exit('error');
			}
			if(@$_SESSION['service_img_record']){
				if(count(json_decode($_SESSION['service_img_record'],1)) >= 5){
					exit('e3');
				}
			}
			$arr_ext = array('jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF');
			$arr_type = array('image/png','image/jpeg','image/gif');
			$size = 1024000;		//设置图片上传的限制大小
			$file = $_FILES['file'];

			$ext = pathinfo($file['name'],PATHINFO_EXTENSION);
			if(!in_array($ext,$arr_ext)){
				exit('e1');		//后缀不正确
			}
			if(!in_array($file['type'],$arr_type)){
				exit('e2');		//文件类型不正确！
			}
			if($file['size'] > $size){
				exit('e4');		//文件不得大于1M！
			}

			$fsize = $file['size'];
	


			$file_name =  md5(microtime(1).rand(1,9999999)).'_'.$fsize.'.'.$ext;

			$content = file_get_contents($_FILES['file']['tmp_name']);

			require 'Service/Class/upyun.class.php';

			$upyun = new UpYun();

			$path = '/imgs/'.$file_name;
			$res = $upyun->writeFile($path, $content);
			if(count($res) >= 4){
				require 'Service/Class/MyPdo.class.php';
				$mypdo = new MyPdo();
				$sql = 'insert into service_img_record(name,ctime) values(?,?)';
				$res = $mypdo->dml($sql,array($file_name,time()));
				if(@$_SESSION['service_img_record']){
					$arr_img = json_decode($_SESSION['service_img_record'],1);
					array_push($arr_img,$file_name);
					$_SESSION['service_img_record'] = json_encode($arr_img);
				}else{
					$arr_img = array($file_name);
					$_SESSION['service_img_record'] = json_encode($arr_img);
				}
				echo $file_name;
			}

		break;




//-- 微博登陆
		case 'weibo_login':
			require 'Service/Class/weibo.class.php';
			$key = '3327049903';
			$secret = '8347316179bb4445dac0d96f9894fdcc';
			$url = 'http://www.service.top/interface.php?type=weibo_login';	//设置回调地址
			$o = new SaeTOAuthV2($key,$secret);
			$get_code_url = $o->getAuthorizeURL($url);	//拼接获取code的URL地址
			if(@!$_COOKIE['uid']){
				if(!$_GET['code']){
					header('location:'.$get_code_url);	//跳转到微博登陆页面		
				}else{
					$keys['code'] = $_GET['code'];	//登陆后获取的 code
					$keys['redirect_uri'] = $url;
					$auth = $o->getAccessToken($keys);	//获取用户信息
					echo $user_account = 'sina_'.$auth['uid'];
					require 'Service/Class/MyPdo.class.php';
					$pdo = new MyPdo();
					$sql = 'select user_id from tp_user where user_account = ?';
					$res = $pdo->dql($sql,array($user_account),1);
					if($res){
						setCookie('uid',$res['user_id']);
					}else{
						$sql = 'insert into tp_user(user_account,user_ctime) values(?,?)';
						$res = $pdo->dml($sql,array($user_account,time()));
						if($res > 0){
							setCookie('uid',$res);
						}else{
							header('location:service_index.html');
						}
					}
					header('location:service_me.html');
				}
			}else{
				header('location:service_me.html');
			}
		break;







//-- qq 登陆
		case 'qq_login':
			setCookie('uid',1,0,'/');
			header('location:../../../service_me.html');exit;
			exit;
			require_once("Service/Class/Qq/qqConnectAPI.php");
			$qc = new QC();
			$qc->qq_login();
		break;









//-- 信息删除处理
		case 'info_del':
			require 'Service/Class/MyPdo.class.php';
			session_start();
			if(!token_v()){
				exit('error');
			}
			$uid = $_COOKIE['uid'];
			$info_id = I('get.info_id');
			$service_id = I('get.service_id');
			$file_name = I('get.file_name');
			$tableName = get_tableName($service_id);
			$m_Pdo = new MyPdo();
			$sql = "update {$tableName} set info_stat = 4 where info_id = ?";
			$res = $m_Pdo->dml($sql,array($info_id));
			if($res > 0){
				//$p = "Service/Template/Service_me/{$uid}/{$service_id}";
				//del_allFiles($p,1);
				//$p2 = "Service/Template/Service_me/{$uid}/none";
				$p2 = "Service/Template/Service_me/{$uid}";
				del_allFiles($p2,1);
				$p3 = "Service/Template/Service_detail/{$file_name}.html";
				del_allFiles($p3,1);
				echo $res;
			}
		break;


		//验证码验证
		case 'issue_c_code_v':
			session_start();
			$code = strtolower(I('post.code'));
			if($code != strtolower($_SESSION['code'])){
				echo 'e';
			}else{
				echo 'ok';
			}
		break;

//-- 信息发布处理
		case 'issue_c':
			session_start();
			if(!$_COOKIE['uid']){
				alert('请先登陆！');
			}
			require 'Service/Class/MyPdo.class.php';
			if(!token_v()){
				alert('error2');
			}

			$service = explode(',',I('post.service'));	//获取服务id和名称
			if(count($service) < 2){
				alert('获取服务数据失败!');
			}
			//echo I('post.title');exit;
			$p = '/^.{1,50}$/';
			if(!preg_match($p,I('post.title'))){
				alert('标题非法2！');
			}

			$p = '/^.{1,12}$/';
			if(!preg_match($p,I('post.contacts'))){
				alert('联系人添写不正确2！');
			}
			if(!preg_match('/^[1-9][0-9]{6,11}$/',I('post.phone'))){
				alert('电话输入不正确2！');
			}

			$p = '/^.{1,1500}$/';
			if(!preg_match($p,I('post.content'))){
				alert('详情字符数量非法2！');
			}

			//通用基本数据


			$data['info_uid'] = $_COOKIE['uid'];	//用户id
			$data['info_ctime'] = time();
			$data['info_ip'] = $_SERVER['REMOTE_ADDR'];
			$data['info_title'] = I('post.title');
			$data['info_content'] = I('post.content');
			$data['info_contacts'] = I('post.contacts');
			$data['info_phone'] = I('post.phone');

			//城市数据
			$data['info_city1_id'] = $data_find['info_city1_id'] = I('post.city1');
			$data['info_city1'] = '吉林省';
			$data['info_city2_id'] = $data_find['info_city2_id'] = I('post.city2');
			$data['info_city2'] = '通化市';
			$data['info_city3_id'] = $data_find['info_city3_id'] = I('post.city3');
			$data['info_city3'] = '柳河县';
			//服务分类数据
			$data['info_service_type_id'] = $data_find['info_service_type_id'] = $service[0];
			$data['info_service_type'] = $service[1];

			//类别
			$type = I('post.type');
			$type_arr = explode(',',$type);
			$data['info_cond_type_id'] = $type_arr[0];
			$data['info_cond_type'] = $type_arr[1];

			//行业
			if($industry = I('post.industry')){
				$industry_arr = explode(',',$industry);
				$data['info_cond_industry_id'] = $industry_arr[0];
				$data['info_cond_industry'] = $industry_arr[1];
			}

			//户型
			if($shape = I('post.shape')){
				$shape_arr = explode(',',$shape);
				$data['info_cond_shape_id'] = $shape_arr[0];
				$data['info_cond_shape'] = $shape_arr[1];
			}

			//物品类型
			if($goods = I('post.goods')){
				$goods_arr = explode(',',$goods);
				$data['info_cond_goods_type_id'] = $goods_arr[0];
				$data['info_cond_goods_type'] = $goods_arr[1];
			}

			//宠物分类
			if($animal = I('post.animal')){
				$animal_arr = explode(',',$animal);
				$data['info_cond_animal_type_id'] = $animal_arr[0];
				$data['info_cond_animal_type'] = $animal_arr[1];
			}

			//价格或薪资

			if($price = I('post.price')){
				if(!preg_match('/^[1-9][0-9]{0,9}$/',$price)){
					alert('价格或薪资输入不正确2！');
				}
				switch($service[1]){
					case '出兑信息':
						if($price == 0){
							$price_id = 0;
						}else if($price < 5000){
							$price_id = 159;
						}else if($price >= 5000 && $price < 10000){
							$price_id = 160;
						}else if($price >= 5000 && $price < 10000){
							$price_id = 160;
						}else if($price >= 10000 && $price < 50000){
							$price_id = 161;
						}else if($price >= 50000 && $price < 100000){
							$price_id = 162;
						}else{
							$price_id = 163;
						}
					break;
					case '宠物交易':
						if($price == 0){
							$price_id = 0;
						}else if($price < 100){
							$price_id = 171;
						}else if($price >= 100 && $price < 500){
							$price_id = 172;
						}else if($price >= 500 && $price < 1000){
							$price_id = 173;
						}else if($price >= 1000 && $price < 5000){
							$price_id = 174;
						}else if($price >= 5000 && $price < 10000){
							$price_id = 175;
						}else if($price >= 10000 && $price < 50000){
							$price_id = 176;
						}else if($price >= 50000 && $price < 100000){
							$price_id = 177;
						}else{
							$price_id = 178;
						}
					break;
					case '房屋租售':
						if($price == 0){
							$price_id = 0;
						}else if($price < 500){
							$price_id = 347;
						}else if($price >= 500 && $price < 1000){
							$price_id = 348;
						}else if($price >= 1000 && $price < 2000){
							$price_id = 349;
						}else if($price >= 2000 && $price < 5000){
							$price_id = 350;
						}else if($price >= 5000 && $price < 10000){
							$price_id = 351;
						}else if($price >= 10000 && $price < 100000){
							$price_id = 352;
						}else if($price >= 100000 && $price < 200000){
							$price_id = 339;
						}else if($price >= 200000 && $price < 300000){
							$price_id = 340;
						}else if($price >= 300000 && $price < 500000){
							$price_id = 341;
						}else if($price >= 500000 && $price < 1000000){
							$price_id = 342;
						}else if($price >= 1000000 && $price < 2000000){
							$price_id = 343;
						}else if($price >= 2000000 && $price < 5000000){
							$price_id = 344;
						}else if($price >= 5000000 && $price < 10000000){
							$price_id = 345;
						}else if($price > 10000000){
							$price_id = 346;
						}

					break;
					case '二手物品':
						if($price == 0){
							$price_id = 0;
						}else if($price < 50){
							$price_id = 258;
						}else if($price >= 50 && $price < 100){
							$price_id = 259;
						}else if($price >= 100 && $price < 200){
							$price_id = 260;
						}else if($price >= 200 && $price < 500){
							$price_id = 261;
						}else if($price >= 500 && $price < 1000){
							$price_id = 262;
						}else if($price >= 1000 && $price < 2000){
							$price_id = 263;
						}else if($price >= 2000 && $price < 5000){
							$price_id = 264;
						}else{
							$price_id = 265;
						}
					break;
					case '求职招聘':
						if($price == 0){
							$price_id = 0;
						}else if($price < 2000){
							$price_id = 10;
						}else if($price >= 2000 && $price < 5000){
							$price_id = 11;
						}else if($price >= 5000 && $price < 10000){
							$price_id = 12;
						}else if($price >= 10000 && $price < 15000){
							$price_id = 13;
						}else{
							$price_id = 14;
						}
					break;
					case '二手车辆':
						if($price == 0){
							$price_id = 0;
						}else if($price < 10000){
							$price_id = 201;
						}else if($price >= 10000 && $price < 20000){
							$price_id = 202;
						}else if($price >= 20000 && $price < 50000){
							$price_id = 203;
						}else if($price >= 50000 && $price < 100000){
							$price_id = 204;
						}else if($price >= 100000 && $price < 200000){
							$price_id = 205;
						}else if($price >= 200000 && $price < 500000){
							$price_id = 206;
						}else if($price >= 500000 && $price < 1000000){
							$price_id = 207;
						}else{
							$price_id = 208;
						}
					break;
				}
				$data['info_cond_price_id'] = $price_id;
				$data['info_cond_price'] = $price;
			}else{
				if($service[1] != '寻物寻人' ){
					$data['info_cond_price_id'] = 0;
					$data['info_cond_price'] = 0;
				}
			}
			//面积 
			if($service[1] == '房屋租售' ){
				if(!preg_match('/^[1-9][0-9]{0,4}(?:[1-9]?|[1-9]?[.](?:[1-9]|[0-9][1-9]))$/',I('post.area'))){
					alert('面积输入非法2！');
				}
				$area = I('post.area');
				if($area < 50){
					$area_id = 55;
				}else if($area >= 50 && $area < 80){
					$area_id = 56;
				}else if($area >= 80 && $area < 120){
					$area_id = 57;
				}else if($area >= 120 && $area < 200){
					$area_id = 58;
				}else if($area >= 200 && $area < 300){
					$area_id = 59;
				}else{
					$area_id = 60;
				}

				$data['info_cond_area_id'] = $area_id;
				$data['info_cond_area'] = $area;
			
			}

			//车辆
			if($service[1] == '二手车辆' ){
				$data['info_color'] = I('post.color');

				//上牌时间
				if(I('post.register_time_year') == 'other'){
					$data['info_register_time'] = '其它';
				}else{
					$data['info_register_time'] = I('post.register_time_year').'年'.I('post.register_time_month').'月';
				}

				//行驶里程 
				if(!preg_match('/^[1-9][0-9]{0,6}$/',I('post.mileage'))){
					//echo "<script>alert('里程输入非法2！');history.go(-1);</script>";exit;
					alert('里程输入非法2！');
				}
				$data['info_mileage'] = I('post.mileage');
			

				//年检到期
				if(I('post.check_time_year') == 'other'){
					$data['info_check_time'] = '其它';
				}else{
					$data['info_check_time'] = I('post.check_time_year').'年'.I('post.check_time_month').'月';
				}

				//交强险到期 
				if(I('post.insurance_traffic_year') == 'other'){
					$data['info_insurance_traffic'] = '其它';
				}else{
					$data['info_insurance_traffic'] = I('post.insurance_traffic_year').'年'.I('post.insurance_traffic_month').'月';
				}
			

				//商业险到期 
				if(I('post.insurance_business_year') == 'other'){
					$data['info_insurance_business'] = '其它';
				}else{
					$data['info_insurance_business'] = I('post.insurance_business_year').'年'.I('post.insurance_business_month').'月';
				}
				
				//看车地址 
				if(!preg_match('/^.{0,150}$/',I('post.look_addr'))){
					//echo "<script>alert('看车地址输入超限2！');history.go(-1);</script>";exit;
					alert('看车地址输入超限2！');
				}
				$data['info_look_addr'] = I('post.look_addr');
				

			}


			//学历 
			if($edu = I('post.edu')){
				$edu_arr = explode(',',$edu);
				$data['info_cond_edu_id'] = $edu_arr[0];
				$data['info_cond_edu'] = $edu_arr[1];
			}

			//经验 
			if($experience = I('post.experience')){
				$experience_arr = explode(',',$experience);
				$data['info_cond_experience_id'] = $experience_arr[0];
				$data['info_cond_experience'] = $experience_arr[1];
			}

			//车型
			if($car_type = I('post.car_type')){
				$car_type_arr = explode(',',$car_type);
				$data['info_cond_car_type_id'] = $car_type_arr[0];
				$data['info_cond_car_type'] = $car_type_arr[1];
			}

			//车辆品牌
			if($brand = I('post.brand')){
				$brand_arr = explode(',',$brand);
				$data['info_cond_brand_id'] = $brand_arr[0];
				$data['info_cond_brand'] = $brand_arr[1];
			}

			if($service[1] != '求职招聘'){
				if(count(json_decode($_SESSION['service_img_record'],1)) > 0){
					$data['info_imgs'] = $_SESSION['service_img_record'];
					
				}
			}
		  	$tableName = get_tableName($service[0]);

			$m_Pdo = new MyPdo();
			//判断用户当天是否发过信息
			foreach($data_find as $k=>$v){
					$keys_find[] = $k;
					$vals_find[] = $v;
			}
			$keys_find = implode('=? and ',$keys_find);
			if(strlen($keys_find) > 0){
				$keys_find .= '=? and info_ctime>'.get_today_start_timestamp();
			}
 			$sql_find = "select * from {$tableName} where {$keys_find} ";
			$res = $m_Pdo->dql($sql_find,$vals_find);
			if($res){
				//alert('每天只能发布一条同类信息！');
			}


			//设置添加数据
			foreach($data as $k=>$v){
				$keys[] = $k;
				$vals[] = $v;
				$marks[] = '?';
			}
			$keys = implode(',',$keys);
			$marks = implode(',',$marks);
			$sql = "insert into {$tableName}({$keys}) values({$marks})";
			$st = $m_Pdo->dml($sql,$vals);
			if($st){
				$sql_user_issue_infos_find = "select infos from tp_user_issue_infos where uid = ?";
				$st_user_issue_infos_find = $m_Pdo->dql($sql_user_issue_infos_find,array($_COOKIE['uid']),1);

				$user_issue_infos['service_id'] = $service[0];
				$user_issue_infos['info_id'] = $st;

				if($st_user_issue_infos_find){	//如果以有记录
					$data_tmp = json_decode($st_user_issue_infos_find['infos'],1);
					//$data_tmp[] = $user_issue_infos;
					array_unshift($data_tmp, $user_issue_infos);
					$user_issue_data_json = json_encode($data_tmp);
					$sql_user_issue_infos_insert = "update tp_user_issue_infos set infos= ? where uid = ?";
					$st_user_issue_insert = $m_Pdo->dml($sql_user_issue_infos_insert,array($user_issue_data_json,$_COOKIE['uid']));
				}else{	//如果没有记录
					$user_issue_data[] = $user_issue_infos;
					$user_issue_data_json = json_encode($user_issue_data);
					$sql_user_issue_infos_insert = "insert into tp_user_issue_infos(uid,infos) values(?,?)";
					$st_user_issue_insert = $m_Pdo->dml($sql_user_issue_infos_insert,array($_COOKIE['uid'],$user_issue_data_json));
				}
				$_SESSION['me_infos'] = '';
				$sql_user_issue_infos = "insert into tp_user_issue_infos(uid,iinfos) values(?,?)";
				$img_arr = json_decode($_SESSION['service_img_record'],1);
				foreach($img_arr as $v){
					$sql = 'delete from service_img_record where name = ?';
					$res = $m_Pdo->dml($sql,array($v));
				}
				$_SESSION['service_img_record'] = '';
				$p = 'Service/Template/Service_me/'.$data['info_uid'];
				del_allFiles($p,1);
				//$p2 = 'Service/Template/Service_me/'.$data['info_uid'].'/none';
				//del_allFiles($p2,1);
				echo "<script>sessionStorage.issue_img_upload='';</script>";
				alert('发布成功！','service_index.html');
			}else{
				var_dump($m_Pdo->error());
			}
		break;




//-- 验证码
		case 'code':
			header("content-type:text/html;charset=utf-8");
			session_start();
			$img=imagecreatetruecolor(120,50);
			$bgcolor=imagecolorallocate($img,255,255,0);
			imagefill($img,0,0,$bgcolor);
			for($i=0;$i<700;$i++){
				$pcolor=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
				$x=rand(0,120);
				$y=rand(0,50);
				imagesetpixel($img,$x,$y,$pcolor);
			}
			$str='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$text='';
			for($i=0;$i<4;$i++){
				$text.=$str[rand(0,strlen($str)-1)];
			}
			$tcolor=imagecolorallocate($img,255,0,255);
			$_SESSION['code'] = $text;
			imagettftext($img,30,0,32,35,$tcolor,'./Service/Fonts/andlso.ttf',$text);
			header('content-type:image/jpeg');
			imagejpeg($img);
			imagedestroy($img);
		break;




//-- token设置
		case 'token':
			session_start();
			echo tokens();
		break;


		case 'token_v':
			if(token_v()){
				echo 'ok';
			}else{
				echo 'err';
			}
		break;

	}


	

//设置并返回token值 
function tokens(){
    $val = md5(md5(microtime()).md5(microtime(1)).rand(1,99999999));
    $_SESSION['token'] = $val;
    return $_SESSION['token'];
}

function token_v(){
	//session_start();
	if(@$_POST['token']){
		$token = htmlspecialchars(@$_POST['token']);
	}else{
		$token = htmlspecialchars(@$_GET['token']);
	}
	$p = '/www.service.top/';
	if(!preg_match($p,@$_SERVER['HTTP_REFERER']) || !$token || $token != $_SESSION['token']){
		return false;
	}else{
		return true;
	}
}


//dump() 展示数据
function dump($arr){
	echo '<pre>';
		var_dump($arr);
	echo '</pre>';
}




//I() 过滤POST,GET数据
function I($name){
	error_reporting(0);
	$arr = explode('.',$name);;
	if(count($arr) == 1){			//如果只给名字，则获取post,get 方式的数据
		if($_POST[$name]){
			return htmlspecialchars(trim($_POST[$name]));
		}
		if($_GET[$name]){
			return htmlspecialchars(trim($_GET[$name]));
		}
	}
	if(empty($arr[1])){
		switch($arr[0]){
			case 'post':
			case 'POST':
				foreach($_POST as $k=>$v){
					$k2=htmlspecialchars(trim($k));
					$v2=htmlspecialchars(trim($v));
					$data[$k2] = $v2;
				}
				return $data;
			break;
			
			case 'get':
			case 'GET':
				foreach($_GET as $k=>$v){
					$k2=htmlspecialchars(trim($k));
					$v2=htmlspecialchars(trim($v));
					$data[$k2] = $v2;
				}
				return $data;
			break;
		}
	}
	switch($arr[0]){
		case 'post':
		case 'POST':
			return htmlspecialchars(trim($_POST[$arr[1]]));
		break;
		case 'get':
		case 'GET':
			return htmlspecialchars(trim($_GET[$arr[1]]));
		break;
	}
}



//expire_v() 验证时间是否过期
/*function expire_v($name){
	global $config;
	$expire = intval(S($name));
	if(!is_int($expire)){
		return false;
	}
	if($expire < time()-$config['CACHE_EXPIRE']){
		return false;
	}else{
		return true;
	}
}
*/


//S() 缓存数据
function S($name,$val=''){
	$path = 'Service/Cache/service_timestamp';
	if($val === ''){			//查询
		if(!is_file($path)){
			return ;
		}else{
			$data = file_get_contents($path);
			$data_arr = unserialize($data);
			if(array_key_exists($name,$data_arr)){
				return $data_arr[$name];
			}else{
				return ;
			}
		}
	}else{		//设置
		if(!is_file($path)){
			$data = array($name=>$val);
			$str = serialize($data);
			file_put_contents($path, $str);
		}else{
			$data = file_get_contents($path);
			$data_arr = unserialize($data);
			$data_arr[$name] = $val;
			$str = serialize($data_arr);
			file_put_contents($path, $str);
		}	
	}
}



//alert() 弹出提示并返回上页
function alert($msg,$type=1){
	//1 返回上一页
	if($type==1){
		echo "<script>alert('{$msg}');history.go(-1);</script>";exit;
	}else{
		echo "<script>alert('{$msg}');location.href='{$type}';</script>";exit;
	}
}


//get_today_start_timestamp() 获取当天起始的时间戳
function get_today_start_timestamp(){
	$t = Date('Y-m-d',time());
	return $t2 = strtotime($t);
}





//根据服务id或服务名 获取表名
function get_tableName($sid){
	if(!$_SESSION['service_list']){
		$mp = new MyPdo();
		$sql = "select * from service_type";
		$res = $mp->dql($sql,array());
		$_SESSION['service_list'] = $res;
	}
	foreach($_SESSION['service_list'] as $v){
		if($sid == $v['type_id']){
			return $v['table_name'];
		}
	}
}




/*
	遍历删除目录中所有文件
	@param 	$p 	文件名或路径
	@param 	$type 	是不提示文字，默认为空显示提示
*/
function del_allFiles($p,$type=''){
    if(is_dir($p)){
        $od = opendir($p);
        while($rd = readdir($od)){
            if($rd == '.' || $rd == '..'){
                continue;
            }
            $p2 = $p.'/'.$rd;
            del_allFiles($p2,$type);
        }
    }else{
        unlink($p);
        if($type == ''){
        	echo $p.'已删除！<br>';
        }
    }
}
